//
//  ZGLAdviceVC.m
//  Faker
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLAdviceVC.h"
#import <MessageUI/MessageUI.h>


@interface ZGLAdviceVC ()<MFMessageComposeViewControllerDelegate, UITextViewDelegate>
@property (nonatomic, copy) NSString *inputText;
@property (nonatomic, retain) UITextView *textView;

@end

@implementation ZGLAdviceVC
-(void)dealloc {
    self.textView.delegate = nil;
}
-(void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0.824 alpha:1.000];
    self.navigationItem.title = @"意见反馈";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zaihufanhui"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItem)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    [self setMessage];
    // Do any additional setup after loading the view.
}
-(void)leftBarButtonItem {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 4 - UITextView创建
-(void)setMessage {
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20 * ScaleX, 100 * ScaleX,  335 * ScaleX, 220 * ScaleX)];
    self.textView.layer.cornerRadius = 10;
    self.textView.layer.masksToBounds = YES;
    self.textView.text = @"请提出您的宝贵意见";
    self.textView.textColor = [UIColor lightGrayColor];
    self.textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.textView.layer.borderWidth = 0.3;
    [self.view addSubview:self.textView];
    self.textView.delegate = self;
    /* 自适应高度 */
    self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];//设置字体名字和字体大小
    self.textView.returnKeyType = UIReturnKeyDefault;//返回键的类型
    self.textView.keyboardType = UIKeyboardTypeDefault;//键盘类型
    self.textView.scrollEnabled = YES;//是否可以拖动
    
}
#pragma mark - 5 UITextView协议方法实现
/* 开始编辑时*/
- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:@"请提出您的宝贵意见"]) {
        textView.text = @"";
    }
    /* 创建右边button */
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)];
    self.navigationItem.rightBarButtonItem = done;
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}
/* 结束编辑时  图标赋空值*/
- (void)textViewDidEndEditing:(UITextView *)textView {
    
    self.navigationItem.rightBarButtonItem = nil;
    
}
#pragma mark 6 - 发送消息到手机功能实现
/* 将输入的值做参数进行传值 */
- (void)leaveEditMode {
    [self showMessageView:[NSArray arrayWithObject:@15214593480] title:nil body:self.inputText];
    
}
/* 获取用户输入的文本 */
-(void)textViewDidChange:(UITextView *)textView {
    self.inputText = textView.text;
    
}
/* 发送结果显示 */
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissViewControllerAnimated:YES completion:nil];
    switch (result) {
        case MessageComposeResultSent:
            //信息传送成功
            break;
        case MessageComposeResultFailed:
            //信息传送失败
            break;
        case MessageComposeResultCancelled:
            //信息被用户取消传送
            break;
        default:
            break;
    }
}
/* 信息发送过程 */
-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
    if( [MFMessageComposeViewController canSendText] )
    {
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
        controller.recipients = phones;
        controller.navigationBar.tintColor = [UIColor redColor];
        controller.body = body;
        controller.messageComposeDelegate = self;
        [self presentViewController:controller animated:YES completion:nil];
        [[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面标题
    }
    else
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"该设备不支持短信功能" preferredStyle:1];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:0 handler:^(UIAlertAction *action) {
        }];
        [alertController addAction:alertAction];
        [self presentViewController:alertController animated:YES completion:nil];
        
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
